package net.youdi.gateway.handler;

import net.youdi.gateway.context.SensitiveContext;
import org.springframework.boot.ApplicationArguments;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author: scl
 * @date:2020-04-03 11:43
 * @description
 */
@Component
public class FromMessageClientSensitiveCacheHandler implements SensitiveCacheHandler {
    private static String GET_SENSITIVE_URL = "http://message/sensitive/word/list";
    private RestTemplate restTemplate;

    public FromMessageClientSensitiveCacheHandler(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Override
    public void run(ApplicationArguments args) {
        try {
            loadSensitiveWords();
        } catch (Exception e){}
    }

    @Async
    @Override
    public void loadSensitiveWords(){
        /*List<String> list = this.webClient
                .baseUrl(GET_SENSITIVE_URL)
                .build()
                .method(HttpMethod.GET)
                .retrieve().bodyToMono(List.class).block();*/
        List<String> list = restTemplate.getForObject(GET_SENSITIVE_URL,List.class);
        SensitiveContext.addSensitiveWordToHashMap(list);
    }
}
